/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/19
 * Time: 20:18
 */
$(function(){
    loadList('/v1/manager/program/user/'+program_id+'/apart/list.json');

    $('#change-status-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var status = button.data('change-status');

        $(this).find('.program-user-id').val(id);
        $(this).find('.program-user-status').val(status);
        if (status == 1) {
            $(this).find('.check-user-tip').text('您确定通过吗');
        }
        if (status == 2) {
            $(this).find('.check-user-tip').text('您确定拒绝吗');
        }
    });
    $('#change-status-dialog .btn-save').on('click', function() {
        var data = {
            id: $('.program-user-id').val(),
            status: $('.program-user-status').val()
        };

        sendAjax('/v1/user/apart/change/status', 'post', {data:data}, function(json){
            $('#change-status-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
            var flag = data.status == 1 ? 3 : 4;
            sendAjax('/v1/manager/send/msg', 'post', {user_id:data.id, flag:flag}, function(json){});
        });
    });
    $('#change-status-dialog').on('hide.bs.modal', function(){
        $(this).find('.program-user-id').val('');
        $(this).find('.program-user-status').val('');
        $(this).find('.set-select-time').addClass('hidden');
        $(this).find('.start-end-time').val('').attr('disabled', 'disabled');
    });

    $('#action-tip').on('hide.bs.modal', function(){
        location.reload();
    });
});

function loadList(url) {
    $("#user-apart-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'realname',
            title: '选房人'
        }, {
            field: ' apart_id',
            title: '房源ID',
            visible: false
        }, {
            field: 'apart_name',
            title: '选择房源'
        },{
            field: 'start_end_time',
            title: '选房时间'
        },  {
            field: 'status',
            title: ' 状态',
            formatter: function (value) {
                switch (value) {
                    case 0 : return '待审核';
                    case 1 : return '已通过';
                    case 2 : return '未通过';
                    default : return '待审核';
                }
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                return row.status == 1 ? '<a class="btn btn-xs btn-danger" data-toggle="modal" data-target="#change-status-dialog" data-change-status="2" data-id="'+row.id+'">拒绝</a>' : '<a class="btn btn-xs btn-success" data-toggle="modal" data-target="#change-status-dialog" data-change-status="1" data-id="'+row.id+'">通过</a>';
            }
        }]
    });
}